//
//  ChartCategoryDetailDateBaseView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 09.04.25.
//

import SwiftUI

struct ChartCategoryDetailDateBaseView: View {
    @State var trendTitle: String = ""
    @State var averageTitle: String = ""
    @Binding var date: Date
    @State var dateType: DateSelectType
    @Binding var totalAmount: Double
    @Binding var averageAmount: Double
    @Binding var trendData: [(day:String, amount:Double)]
    @Binding var categoryDetailList: [AccountingDetailCombine]
    @Binding var category: AccountingCategory
    @Binding var orderBy: String
    
    var loadData: () -> Void
    
    var body: some View {
        ScrollView {
            DateSelectView(
                date: $date,
                dateType: dateType,
                isFullWidth: true,
                leftClick: { date in
                    loadData()
                },
                rightClick: { date in
                    loadData()
                }
            )
            .padding(.horizontal, 20)
            
            ChartTrendView(
                trendTitle: $trendTitle,
                averageTitle: $averageTitle,
                totalAmount: $totalAmount,
                averageAmount: $averageAmount,
                data: $trendData
            )
            .padding(EdgeInsets(top: 10, leading: 20, bottom: 5, trailing: 20))
            
            HStack {
                Text("支出明细")
                    .font(.system(size: 14))
                    .bold()
                    .foregroundStyle(Color.koiColorTitle)
                Spacer()
                Button(action: {
                    orderBy = "date"
                    loadData()
                }) {
                    Text("时间")
                        .font(.system(size: 12))
                        .foregroundColor(orderBy == "date" ? Color.koiColorTitle : Color.koiColorTitleSecond)
                        .fontWeight(orderBy == "date" ? .medium : .regular)
                }
                Color.koiColorDivider.frame(width: 1,height: 10)
                Button(action: {
                    orderBy = "amount"
                    loadData()
                }) {
                    Text("金额")
                        .font(.system(size: 12))
                        .foregroundColor(orderBy == "amount" ? Color.koiColorTitle : Color.koiColorTitleSecond)
                        .fontWeight(orderBy == "amount" ? .medium : .regular)
                }
            }
            .padding(EdgeInsets(top: 10, leading: 20, bottom: 10, trailing: 20))
            
            if !categoryDetailList.isEmpty {
                LazyVStack {
                    ForEach(categoryDetailList) { item in
                        ChartCategoryDetailItem(item: item)
                    }
                }
                .padding(16)
                .background(Color.koiColorBackgroundItem)
                .cornerRadius(16)
                .padding(EdgeInsets(top: 5, leading: 20, bottom: 5, trailing: 20))
            }
            // 底部占位
            Color.clear.frame(height: 90)
        }
        .onAppear {
            loadData()
            trendTitle = getTrendTitle()
            averageTitle = getAverageTitle()
        }
    }
    
    private func getTrendTitle() -> String {
        var title = "本周"
        switch dateType {
        case .week:
            title = "本周"
        case .month:
            title = "本月"
        case .year:
            title = "本年"
        }
        return "\(title)\(getTypeText())"
    }
    
    private func getAverageTitle() -> String {
        switch dateType {
        case .week:
            return "日均\(getTypeText())"
        case .month:
            return "日均\(getTypeText())"
        case .year:
            return "月均\(getTypeText())"
        }
    }
    
    private func getCategoryTitle() -> String {
        return "类别\(getTypeText())统计"
    }
    
    private func getTypeText() -> String {
        if category.getCategoryType() == .income {
            return "收入"
        } else {
            return "支出"
        }
    }
}

//#Preview {
//    ChartCategoryDetailDateBaseView()
//}
